package kingwing.dc.core.view.rule.excel.importprocessor;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import kingwing.dc.core.model.Rule;
import kingwing.dc.core.model.RuleDetail;
import kingwing.dc.core.orm.hibernate.CommonHibernateDao;

import org.springframework.stereotype.Component;

import com.bstek.bdf2.importexcel.model.CellWrapper;
import com.bstek.bdf2.importexcel.model.ExcelDataWrapper;
import com.bstek.bdf2.importexcel.model.RowWrapper;
import com.bstek.bdf2.importexcel.processor.IExcelProcessor;

@Component
public class RuleIExcelProcessor implements IExcelProcessor {

	@Resource(name=CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao commonHibernateDao;
	
	@Override
	public String getName() {
		return null;
	}

	/**
	 * 0:导入成功；-1：导入出错；
	 * -2：excel数据验证不通过；-12：数据行验证不通过；-22：数据列验证不通过
	 * 
	 */
	@Override
	public int execute(ExcelDataWrapper excelDataWrapper)  {
		Rule rule = new Rule();
		List<RuleDetail> ruleDetails = new ArrayList<RuleDetail>();
		RuleDetail ruleDetail = new RuleDetail();
		DecimalFormat decimalFormat = new DecimalFormat("#"); 
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("标题", 0);
		map.put("正文", 1);
		map.put("时间", 2);
		map.put("URL", 3);
		map.put("列表", 4);
		
		try {
			//判断验证是否通过
			if (!excelDataWrapper.isValidate()) {
				return -2;
			}
			//获取excel数据
			Collection<RowWrapper> rowWrappers = excelDataWrapper.getRowWrappers();
			
			for (RowWrapper rowWrapper : rowWrappers) {
				
				//判断验证是否通过
				if (!rowWrapper.isValidate()) {
					return -12;
				}
				//获取数据行数据
				Collection<CellWrapper> cellWrappers = rowWrapper.getCellWrappers();
				
				for (CellWrapper cellWrapper : cellWrappers) {
					//判断验证是否通过
					if (!cellWrapper.isValidate()) {
						return -22;
					}
					
					switch (rowWrapper.getRow()) {
					case 1:
						if (cellWrapper.getColumn()==2) {
							rule.setRuleName(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==4) {
							rule.setDomains(cellWrapper.getValue().toString());
						}
						break;
					case 2:
						if (cellWrapper.getColumn()==2) {
							String pageTypeString = cellWrapper.getValue().toString();
							if (this.isInteger(pageTypeString)) {
								int pageType = decimalFormat.parse(cellWrapper.getValue().toString()).intValue();
								rule.setPageType(pageType);
							}else {
								int pageType = 0;
								if (pageTypeString.equals("静态页面")) {
									pageType = 0;
								}else if (pageTypeString.equals("动态页面")) {
									pageType = 1;
								}
								rule.setPageType(pageType);
							}
						}
						if (cellWrapper.getColumn()==4) {
							String grabTypeString = cellWrapper.getValue().toString();
							if (this.isInteger(grabTypeString)) {
								int grabType = decimalFormat.parse(grabTypeString).intValue();
								rule.setGrabType(grabType);
							}else {
								int grabType = 0;
								if (grabTypeString.equals("列表+详细内容页")) {
									grabType = 0;
								}else if (grabTypeString.equals("单页面采集")) {
									grabType = 1;
								}
								rule.setGrabType(grabType);
							}
						}
						break;
					case 3:
						if (rule.getPageType()==0) {
							if (cellWrapper.getColumn()==2) {
								rule.setScanUrls(cellWrapper.getValue().toString());
							}
						}else{
							if (cellWrapper.getColumn()==2) {
								rule.setContentUrls(cellWrapper.getValue().toString());
							}
							if (cellWrapper.getColumn()==4) {
								rule.setContentId(cellWrapper.getValue().toString());
							}
						}
						break;
					case 4:
						if (rule.getPageType()==0) {
							if (cellWrapper.getColumn()==2) {
								rule.setContentUrlRegexes(cellWrapper.getValue().toString());
							}
						}else {
							if (cellWrapper.getColumn()==2) {
								rule.setScanUrls(cellWrapper.getValue().toString());
							}
						}
						break;
					case 5:
						if (rule.getPageType()==0) {
							if (cellWrapper.getColumn()==2) {
								rule.setHelperUrlRegexes(cellWrapper.getValue().toString());
							}
						}else {
							if (cellWrapper.getColumn()==2) {
								rule.setContentUrlRegexes(cellWrapper.getValue().toString());
							}
						}
						break;
					case 6:
						if (rule.getPageType()==0) {
							
						}else {
							if (cellWrapper.getColumn()==2) {
								rule.setHelperUrlRegexes(cellWrapper.getValue().toString());
							}
						}
						break;
					case 10:
						if (rule.getPageType()==0) {
							if (cellWrapper.getColumn()==1) {
								ruleDetail.setRuleDetailName(cellWrapper.getValue().toString());
							}
							if (cellWrapper.getColumn()==2) {
								ruleDetail.setRuleDetailSelector(cellWrapper.getValue().toString());
							}
							if (cellWrapper.getColumn()==3) {
								ruleDetail.setFieldType(map.get(cellWrapper.getValue().toString()));
							}
							if (cellWrapper.getColumn()==4) {
								ruleDetail.setRuleDetailColumn((boolean)cellWrapper.getValue());
							}
						}
						break;
					case 11:
						if (cellWrapper.getColumn()==1) {
							ruleDetail.setRuleDetailName(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==2) {
							ruleDetail.setRuleDetailSelector(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==3) {
							ruleDetail.setFieldType(map.get(cellWrapper.getValue().toString()));
						}
						if (cellWrapper.getColumn()==4) {
							ruleDetail.setRuleDetailColumn((boolean)cellWrapper.getValue());
						}
						break;
					case 12:
						if (cellWrapper.getColumn()==1) {
							ruleDetail.setRuleDetailName(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==2) {
							ruleDetail.setRuleDetailSelector(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==3) {
							ruleDetail.setFieldType(map.get(cellWrapper.getValue().toString()));
						}
						if (cellWrapper.getColumn()==4) {
							ruleDetail.setRuleDetailColumn((boolean)cellWrapper.getValue());
						}
						break;
					case 13:
						if (cellWrapper.getColumn()==1) {
							ruleDetail.setRuleDetailName(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==2) {
							ruleDetail.setRuleDetailSelector(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==3) {
							ruleDetail.setFieldType(map.get(cellWrapper.getValue().toString()));
						}
						if (cellWrapper.getColumn()==4) {
							ruleDetail.setRuleDetailColumn((boolean)cellWrapper.getValue());
						}
						break;
					case 14:
						if (cellWrapper.getColumn()==1) {
							ruleDetail.setRuleDetailName(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==2) {
							ruleDetail.setRuleDetailSelector(cellWrapper.getValue().toString());
						}
						if (cellWrapper.getColumn()==3) {
							ruleDetail.setFieldType(map.get(cellWrapper.getValue().toString()));
						}
						if (cellWrapper.getColumn()==4) {
							ruleDetail.setRuleDetailColumn((boolean)cellWrapper.getValue());
						}
						break;
					case 15:
						if (rule.getPageType()==0) {
							
						}else {
							if (cellWrapper.getColumn()==1) {
								ruleDetail.setRuleDetailName(cellWrapper.getValue().toString());
							}
							if (cellWrapper.getColumn()==2) {
								ruleDetail.setRuleDetailSelector(cellWrapper.getValue().toString());
							}
							if (cellWrapper.getColumn()==3) {
								ruleDetail.setFieldType(map.get(cellWrapper.getValue().toString()));
							}
							if (cellWrapper.getColumn()==4) {
								ruleDetail.setRuleDetailColumn((boolean)cellWrapper.getValue());
							}
						}
						break;
					}
					
//					System.out.println("行号："+rowWrapper.getRow()+"； 列号："+cellWrapper.getColumn()+"； 列表名:"+cellWrapper.getColumnName()
//							+"； 列值:"+cellWrapper.getValue()+"； 是否主键:"+cellWrapper.isPrimaryKey());
					
				}
				int row = rowWrapper.getRow();
				if (row==10 || row==11 || row==12 || row==13 || row==14 || row==15) {
					ruleDetails.add(ruleDetail);
					ruleDetail = new RuleDetail();
				}
			}
			
			//进行数据持久化
			this.commonHibernateDao.save(rule);
			if (!ruleDetails.isEmpty()) {
				for (RuleDetail ruleD : ruleDetails) {
					ruleD.setRuleId(rule.getId());
					this.commonHibernateDao.save(ruleD);
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		
		return 0;
	}
	
	/**
	 * 判断是否为整数 
	 * @param str
	 * @return
	 */
	public boolean isInteger(String str) {  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
  }

}
